﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Vision.Common
{
    public static class CmsConfigurationManager
    {
        #region Consts
        const string ConnectionStringsSectionName = "connectionStrings";
        const string AppSettingSectionName = "appSettings";
        #endregion

        #region Fields
        static readonly FileConfigurationSource FileConfigurationSource = null;
        static readonly NameValueCollection appSetting = new NameValueCollection();
        static readonly ConnectionStringSettingsCollection connectionStrings = new ConnectionStringSettingsCollection();
        #endregion

        #region Static Ctor
        static CmsConfigurationManager()
        {
            appSetting = new NameValueCollection();
            appSetting.Add(ConfigurationManager.AppSettings);

            connectionStrings = new ConnectionStringSettingsCollection();
            foreach (ConnectionStringSettings connectionString in ConfigurationManager.ConnectionStrings)
            {
                connectionStrings.Add(connectionString);
            }

            var customConfigurationConfigureData = ConfigurationManager.GetSection(CustomConfigurationConfigureData.SectionName) as ICustomConfigurationConfigureData;
            if (customConfigurationConfigureData != null)
            {              
                var customConfigurationConfigureDataFilePath = customConfigurationConfigureData.GetCustomConfigFilePath();
                if (File.Exists(customConfigurationConfigureDataFilePath)) 
                {
                    try
                    {
                        FileConfigurationSource = new FileConfigurationSource(customConfigurationConfigureDataFilePath, false);

                        var connectionStringsSection = FileConfigurationSource.GetSection(ConnectionStringsSectionName) as ConnectionStringsSection;
                        if (connectionStringsSection != null)
                        {
                            foreach (ConnectionStringSettings _connectionStringSettings in connectionStringsSection.ConnectionStrings)
                            {
                                if (connectionStrings[_connectionStringSettings.Name] != null)
                                    connectionStrings.Remove(_connectionStringSettings.Name);
                                connectionStrings.Add(_connectionStringSettings);
                            }
                        }

                        var appSettingsSection = FileConfigurationSource.GetSection(AppSettingSectionName) as AppSettingsSection;
                        if (appSettingsSection != null)
                        {
                            foreach (KeyValueConfigurationElement keyValueConfiguration in appSettingsSection.Settings)
                            {
                                appSetting[keyValueConfiguration.Key] = keyValueConfiguration.Value;
                            }
                        }
                    }
                    catch
                    { 
                    }
                }
            }
        }
        #endregion

        #region Properties
        public static ConnectionStringSettingsCollection ConnectionStrings
        {
            get
            {
                return connectionStrings;
            }
        }
        public static NameValueCollection AppSettings
        {
            get
            {
                return appSetting;
            }
        }
        #endregion

        #region GetSection
        public static object GetSection(string sectionName)
        {
            return (FileConfigurationSource != null ? FileConfigurationSource.GetSection(sectionName) : ConfigurationManager.GetSection(sectionName))
                ?? ConfigurationManager.GetSection(sectionName);
        }
        #endregion
    }

}